import { ClubConfig } from "../../hall_server/config/ClubConfig";
import { Config } from "../../src/config/Config";
import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class User_Spend_DailyDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;

    public static async add_user_spend_daily(param: object) {
        let userid: number = param["userid"]
        let type: string = param["type"]
        let cost: number = param["cost"]
        let dateStr: string = DateUtil.formatDateStr3(DateUtil.now())
        let sql: string = `select * from t_user_spend_daily where userid= ? and date= ?`
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sqlParams = [userid, dateStr];
        let rows = await client.queryPromise(sql, sqlParams);
        if (rows && rows.length) {
            sql = `update t_user_spend_daily set ${type} = ${type} + ${cost} where userid= ${userid} and date= '${dateStr}'`
            rows = await client.queryPromise(sql, sqlParams);
        } else {
            sql = `insert into t_user_spend_daily(userid,${type},date) values(${userid},${cost},'${dateStr}')`
            rows = await client.queryPromise(sql, sqlParams);
        }
    }

    public static async clear() {
        let now: number = DateUtil.now()
        let delete_time: number = now - Config.OneDay * 100; //保留100天记录
        // let delete_time: number = now - Config.OneDay * 3; //保留100天记录
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_user_spend_daily where date < ?"
        let sqlParams = [DateUtil.formatDateStr3(delete_time)];
        await client.queryPromise(sql, sqlParams);
    }

    public static async get_user_spend_daily(kind: string, startTime: string, endTime: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql = `select  t_user_spend_daily.userid,  t_user_spend_daily.${kind},  t_user_spend_daily.date, headimg, nick_name  from t_user_spend_daily  left join t_users on t_user_spend_daily.userid=t_users.userid where t_user_spend_daily.date >= ? and t_user_spend_daily.date < ? `
        let sqlParams = [startTime, endTime];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("get_user_spend_daily===", sql, sqlParams)
        return results
    }

    public static async get_user_spend_by_date(startTime: string, endTime: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = `select * from t_user_spend_daily where date >= ? and date < ?`
        let sqlParams = [startTime, endTime];
        let results = await client.queryPromise(sql, sqlParams);
        return results
    }
}